﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class TinChiDauRaRepository:ITinChiDauRaRepository
    {
        DangKyPhuEntities entities;

        public TinChiDauRaRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region tableRepo

        #endregion

        public int TongTCTLPhaiHTCuaSV(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTao();
            HeDaoTao heDaoTao = new HeDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            khoiLopRepo.KhoiLop(maSV, out nganh, out heDaoTao, out nienKhoa);
            int? soTC=null;
            int nienKhoaID=nienKhoa.NienKhoaID;
            while (true)
            {
                if (soTC == null || soTC == 0)
                {
                    soTC = (from m in entities.TinChiDauRas
                          where m.NganhID == nganh.NganhID
                          && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                          && m.NienKhoaID == nienKhoaID
                          select m.TongTC).FirstOrDefault();
                    nienKhoaID--;
                }
                else
                    break;
            }
            return soTC.Value;
        }

        public int SoTCBatBuocPhaiHTCuaSV(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTao();
            HeDaoTao heDaoTao = new HeDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            khoiLopRepo.KhoiLop(maSV, out nganh, out heDaoTao, out nienKhoa);
            int? soTC=null;
            int nienKhoaID=nienKhoa.NienKhoaID;
            while (true)
            {
                if (soTC == null || soTC == 0)
                {
                    soTC = (from m in entities.TinChiDauRas
                            where m.NganhID == nganh.NganhID
                            && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                            && m.NienKhoaID == nienKhoaID
                            select m.SoTCBatBuoc).FirstOrDefault();
                    nienKhoaID--;
                }
                else
                    break;
            }
             
            return soTC.Value;
        }

        public int SoTCTuChonPhaiHTCuaSV(string maSV)
        {
            int TongTC = TongTCTLPhaiHTCuaSV(maSV);
            int TCBatBuoc = SoTCBatBuocPhaiHTCuaSV(maSV);
            return (TongTC - TCBatBuoc);
        }


        public int TongTCTLPhaiHTCuaSV_CN2(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTaoRepository().LayNganh2CuaSinhVien(maSV);
            HeDaoTao heDaoTao = new HeDaoTaoRepository().LayHeDaoTaoTuMaSV(maSV);
            NienKhoa nienKhoa = new NienKhoaRepository().LayNienKhoaTuMaSV(maSV);

            var soTC = (from m in entities.TinChiDauRas
                        where m.NganhID == nganh.NganhID
                        && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                        && m.NienKhoaID == nienKhoa.NienKhoaID
                        select m.TongTC).FirstOrDefault().Value;
            return soTC;
        }

        public int SoTCBatBuocPhaiHTCuaSV_CN2(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTaoRepository().LayNganh2CuaSinhVien(maSV);
            HeDaoTao heDaoTao = new HeDaoTaoRepository().LayHeDaoTaoTuMaSV(maSV);
            NienKhoa nienKhoa = new NienKhoaRepository().LayNienKhoaTuMaSV(maSV);

            var soTC = (from m in entities.TinChiDauRas
                        where m.NganhID == nganh.NganhID
                        && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                        && m.NienKhoaID == nienKhoa.NienKhoaID
                        select m.SoTCBatBuoc).FirstOrDefault().Value;
            return soTC;
        }

        public int SoTCTuChonPhaiHTCuaSV_CN2(string maSV)
        {
            int TongTC = TongTCTLPhaiHTCuaSV_CN2(maSV);
            int TCBatBuoc = SoTCBatBuocPhaiHTCuaSV_CN2(maSV);
            return (TongTC - TCBatBuoc);
        }
    }
}
